package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.StrategyConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilerChainService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * projectName: sms-platform02
 *
 * @author: 元
 * time: 2021/10/18 0018 16:44
 * description:黑名单过滤策略实现类
 * 黑名单中的号码,不允许发送
 */
@Service(value = "black")
@Slf4j
public class BlackListServiceImpl implements FilerChainService {

    @Autowired
    private CacheFeign cacheFeign;
    @Autowired
    private PushMsgUtil pushMsgUtil;


    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-黑名单策略],开始执行",standardSubmit);

        //1.获取目标手机号
        String mobile = standardSubmit.getDestMobile();
        //2.封装Redis中对应的Key
        String key = CacheConstants.CACHE_PREFIX_BLACK + mobile;
        //3.根据Key来获取Redis中黑名单手机号
        String blackNumber = cacheFeign.get(key);
        //4.判断,查出的数据为空,则当前发送手机号不属于黑名单,返回true
        if (StringUtils.isEmpty(blackNumber)){
            log.info("[策略模块-黑名单策略],黑名单策略执行成功! mobile={}",standardSubmit,mobile);
            return true;
        }else {
            //5.若查询出数据,则说明当前发送手机号属于黑名单,需要发送消息到MQ中
            log.info("[策略模块-黑名单策略],黑名单策略已执行!,该目标手机号属于黑名单 mobile={}",standardSubmit,mobile);
            System.out.println("发送消息日志到MQ中");
            //6.将日志数据发送到日志Topic队列,让ES保存当前数据
            pushMsgUtil.sendLog(standardSubmit, StrategyConstants.STRATEGY_ERROR_BLACK);
            //7.将StandardReport对象封装,发送到状态报告到Topic队列中,接口模块再给用户推送
            pushMsgUtil.pushReport(standardSubmit,StrategyConstants.STRATEGY_ERROR_BLACK);
            return false;

        }


    }
}
